//Hack for z-index problem with applet
function shadowyIFrame(id, target){
	
	var shimmer = $('<iframe id=' + id + '></iframe')
		.width($(target).outerWidth())
		.height($(target).outerHeight())
		.css("color", "#FFFFFF")
		.css("position", "absolute")
		.css("top", $(target).position().top)
		.css("left", $(target).position().left)
		.css("margin-left", $(target).css('margin-left'))
		.css("margin-top", $(target).css('margin-top'))
		.css("zIndex", "999")
		.attr("frameborder", "0")
		.attr("src", "javascript:false;")
		.css("backgroundColor", "#FFFFFF");
	
	$(document.body).append(shimmer);

}

//Friend Point Table Object
var table = {
		"_object" : null,
		"getInstance" : function(){
			try{
				this.init();
			}catch(err){trace2(err);}
			
			return this._object;
		},
		"init" : function(){
			try{
				if(this._object == undefined){
		            this._object = $('.rankTable tbody');
		        }
			}catch(err){trace2(err);}
		},
		"buildFriendTable" : function(data){
			
			try{
				var obj = this.getInstance();
				
				//clear table
				obj.empty();
				
			    $(data.data).each(function(item, value){

			    	obj.append("<tr id='row_"+ value.fId +"'>" + 
			                          "<td>"+ eval(item + 1) +"</td>" + 
			                          "<td id='"+ value.fId +"'><a target='_blank' href='"+ myLinkURL +"and=" + value.fId + "'>" + value.name +"</td>" +
			                          "<td class='messagePoint numberTableCell enabled'>0</td>" +
			                          "<td class='postPoint numberTableCell enabled'>0</td>" +
			                          "<td class='commentPoint numberTableCell enabled'>0</td>" +
			                          "<td class='likePoint numberTableCell enabled'>0</td>" +
			                          "<td class='totalPoint numberTableCell enabled'>0</td>" +
			                    "</tr>");
			    });
			}catch(err){trace2(err);}
		},
		"addToHTMLTable" : function(data){
			try{
				//run function only when data is a valid type(not null, not undefined, and object type) and has data property and data have item(s)
				if(data != undefined && typeof data == 'object' && data.data != undefined && Object.size(data.data)>0){
	
					var data = data.data;
					//looping friends
					for (var id in data) {
						
						//looping datatype
						for(var flagMode in data[id]){
							
							var type = getTypeByFlagMode(flagMode);
							
							var count = data[id][flagMode];
							
							var weight = weights[type];
							
							var point = count * weight;
	
							this.addPoint(id, type, point);						
						}
	
					}
					//sum total
					this.sumTotal();
					
					//sort
					this.sort();
				}
			}catch(err){trace2(err);}
		},
		"rebuildFriendTable": function(data){
            table.clear();
			table.addToHTMLTable(data);
		},
		
		"getPoint" : function(id, type){
			try{
				if(this._object == undefined){
					this.init();
				}
				return ($('tr#row_'+ id + ' td.'+type+'Point', this._object).text() - 0);
			}catch(err){trace2(err);}
		},
		
		"setPoint" : function(id, type, point){
			try{
				if(this._object == undefined){
					this.init();
				}
		        $('tr#row_'+ id + ' td.'+type+'Point', this._object).text(point);
			}catch(err){trace2(err);}
		},
		
		"addPoint" : function(id, type, point){
			try{
				if(this._object == undefined){
					this.init();
				}
				point = point + 0;
				var prevPoint = this.getPoint(id, type) + 0;
				$('tr#row_'+ id + ' td.'+type+'Point', this._object).text(prevPoint + point);
			}catch(err){trace2(err);}
		},

		"sumTotal" : function(){
			try{
				if(this._object == undefined){
					this.init();
				}
				
				$('tr',this._object).each(function(index, row){
					
					//get all the enabled number cells
					var cells = $('td.enabled', row);
				
					var total = 0;
	   
					//sum total excluding cells which are disabled by checkboxes
					$(cells).each(function(index, element){
						if(index < cells.length - 1){
							total += ($(element).text() - 0);
						}
					});
	   
					//show total
					$(cells).last().text(total);
				   	
				});
			}catch(err){trace2(err);}
		},
		
		"sort": function(){
			try{
				
				//sort using tablesorter http://tablesorter.com
				$("table.rankTable").tablesorter({ 
				// sort on total(7th) order dsc and name(2nd) column order asc  
					sortList: [[6,1], [1,0]],
					widgets: ['zebra']
				});
				
				//display ranking no
				var prevScore = -1;
				var prevRank = 0;
				$("tr", this._object).each(function(index, row){
				    var cols = $(row).children();
				    
				    var score = cols.last().text();
				    if (score == prevScore) {
				       cols.first().text(prevRank);
				    }
				    else {
				        cols.first().text(prevRank + 1);
				        prevScore = score;
				        prevRank = prevRank + 1;
				    }
				
				});
			}catch(err){trace2(err);}
		},
		"filtering":function(threshold){
			try{
				if(this._object == undefined){
					this.init();
				}
				
				$('tr', this._object).each(function(index, element){
					//get total score
					var total = $('td.totalPoint', element).text() - 0;
					//if the total score is bigger than threshold, show otherwise hide the row.
					if(total >= threshold){
						$(element).show();
					}else{
						$(element).hide();
					}
				});
			
			}catch(err){trace2(err);}
		},
		"clear" : function(){
			try{
				if(this._object == undefined){
					this.init();
				}
				$('td.numberTableCell',this._object).text(0);
			}catch(err){trace2(err);}
		}
		
	};